import React from 'react'
import { connect } from 'dva'
import { Card,Table,Spin } from 'antd'



/**
 * NetValuePerformanceList 净值表现统计表
 */
const NetValuePerformanceList = ({ 
  fieldList=[],
  data
}) => {  
  // 表格数据设置
  const dataSource=[]
  fieldList.map((item,index)=>{
    const obj={}
    data&&data.map((it,i)=>{
      obj.id=item.id
      obj.name=item.name
      obj[`${it.id}`]=it[item.id]
    })
    dataSource.push(obj)
  })
  // 表头设置
  const columns=[
    {
      title: '',
      dataIndex: 'name',
      key: 'name',
    }
  ]
  data&&data.map(item=>{
    columns.push({
      title: item.name,
      dataIndex: item.id,
      key: item.id,
      // render: (text, record) =>`${Math.signFigures(record[item.id] * 100)}%`
      render: (text, record) =>{
        record[item.id] = Math.round(record[item.id]*10000)/10000
        if(record[item.id]<0){
          return <span style={{color:'#417505'}}>{Math.signFigures(record[item.id] * 100)}%</span>
        }else{
          return <span style={{color:'#D0021B'}}>{Math.signFigures(record[item.id] * 100)}%</span>
        }
      }
    })
  })  
  
  return <Table dataSource={dataSource} columns={columns} rowKey='id' pagination={false} loading={!data} />
}

/**
 * NetValuePerformance 净值表现
 * 引用组件
 * NetValuePerformanceList 净值表现统计表
 */
class NetValuePerformance extends React.Component {
  state={
    data:this.props.netValuePerformance,
  }
  componentDidMount() {
    this.fetchData(this.props)
  }
  componentWillReceiveProps(nextProps) {
    
    if((nextProps.lPortId!==this.props.lPortId)||(nextProps.cBenchCode!==this.props.cBenchCode)){
      this.fetchData(nextProps)
    }
    if(nextProps.netValuePerformance!==this.props.netValuePerformance){
      this.setState({
        data:nextProps.netValuePerformance,
      })
    }
  }
  fetchData=(props)=>{
    this.props.dispatch({
      type:'combinatorialAnalysis/getNetValuePerformance',
      payload:props
    })
  }
  render() {

    return (
      <Card title='净值表现' className="netValuePerformance">
        <NetValuePerformanceList {...this.state.data} />
      </Card>
      
    )
  }
}

NetValuePerformance.defaultProps={
  lPortId:null,
  cBenchCode:null,
  netValuePerformance:{
    data: [
      {
        id:'port1',
        name:'产品',
        quarterProfit:null,// 季度收益
        yearProfit11:null,// 年收益
        totalProfit:null,// 累计收益
        annualProfit:null,// 年化收益
        annualVolatility:null,// 年化波动率
        maximumRetracement:null,// 最大回撤
      },
      {
        id:'bench1',
        name:'基准',
        quarterProfit:null,// 季度收益
        yearProfit11:null,// 年收益
        totalProfit:null,// 累计收益
        annualProfit:null,// 年化收益
        annualVolatility:null,// 年化波动率
        maximumRetracement:null,// 最大回撤
      }
    ],
    fieldList:[
      {
        id: 'quarterProfit',
        name: '近1季度收益111',
      }, {
        id: 'yearProfit11',
        name: '近1年收益',
      }, {
        id: 'totalProfit',
        name: '成立以来累计收益',
      }, {
        id: 'annualProfit',
        name: '成立以来年化收益',
      }, {
        id: 'annualVolatility',
        name: '成立以来年化波动率',
      }, {
        id: 'maximumRetracement',
        name: '成立以来最大回撤',
      }
    ]
  }
  
}

export default connect()(NetValuePerformance)
